let cart = {
    namespaced: true,
    state: {
        cartList: []
    },
    mutations: {
        // 设置购物车
        setCartList(state, cartList) {
            state.cartList = cartList;
        },
        // 获取选中的购物车列表
        getSelectedCartList() {
            var list = [];
            state.cartList.forEach(cart => {
                if (cart.selected) {
                    list.push(cart)
                }
            })
            return list;
        },
        // 选中购物车
        selectCart(state, data) {
            state.cartList.forEach(cart => {
                if (cart.id == data.id && cart.stockEnough && cart.exists) {
                    cart.selected = data.selected;
                }
            })
        },
        // 全部选中或取消
        selectAllCart(state, selected) {
            state.cartList.forEach(cart => {
                if (cart.stockEnough && cart.exists) {
                    cart.selected = selected;
                }
            })
        }
    },
    getters: {
        // 购物车数量
        cartNum: state => {
            return state.cartList.length;
        },
        // 选择的商品数量
        selectedProductQuantity: state => {
            var totalQuantity = 0;
            state.cartList.forEach(cart => {
                if (cart.selected) {
                    totalQuantity += parseInt(cart.quantity);
                }
            })
            return parseInt(totalQuantity);
        },
        // 选择的商品总金额
        selectedProductTotalAmount: state => {
            var totalAmount = 0;
            state.cartList.forEach(cart => {
                if (cart.selected) {
                    totalAmount += cart.skuInfo.price * cart.quantity;
                }
            })
            return totalAmount.toFixed(2);
        },
        // 全选状态
        isAllSelected: state => {
            var allSelected = true;
            state.cartList.forEach(cart => {
                if (!cart.selected && cart.stockEnough && cart.exists) {
                    allSelected = false;
                }
            })
            return state.cartList.length == 0 ? false : allSelected;
        }
    },
    actions: {
    }
}

export default cart;